package our.pack;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class MediaVideoCameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener{

		 private ImageView reddot;
		 private SurfaceView camcorderView;
	     private SurfaceHolder camcorderHolder;
	     private MediaRecorder recorder;
	     private MetaDataDataSource metaDataDatasource;
	     private File PATH = Environment.getExternalStorageDirectory();
	     private String fullNamePath;
	     private boolean recording = false;
	     private static int id = 1;
	     private boolean previewRunning = false;
	     private MyPoint point;

	     /**
	      * makes a video and saves it in a tempfile
	      */
	     @Override 
	     public void onCreate(Bundle savedInstanceState) { 
	          super.onCreate(savedInstanceState);
	          
	        //get the screen right  
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        setContentView(R.layout.mediavideocamera);
	        
	        //get the point where it's made
	        Intent i = getIntent();
	        Bundle b = i.getExtras();
	        point = (MyPoint)b.get("Point");
	        
	        //db
	        metaDataDatasource = new MetaDataDataSource(this);
	        metaDataDatasource.open();
	        
	        //get the id
	        id = (int) (metaDataDatasource.getLatestId() + 1); 
	        
	        //set the path where to save it
	        fullNamePath = Environment.getExternalStorageDirectory() + "/video" + id + ".mp4";
	        
	        //  
	        reddot = (ImageView) findViewById(R.id.reddot);
	        reddot.setAlpha(0);
	        
	        //make the recorder and initialize it
    		recorder = new MediaRecorder();
    		initTheRecorder();
    		
    		//the surfaceView
	        camcorderView = (SurfaceView) findViewById(R.id.surfaceVideoCamera);
	        camcorderView.setOnClickListener(this);
	        camcorderHolder = camcorderView.getHolder();
	        camcorderHolder.addCallback(this);
	  		camcorderHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	  		recorder.setPreviewDisplay(camcorderHolder.getSurface());
	     } 
	     
	     //initialize the recorder
	     public void initTheRecorder(){
	            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
	 	  		recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
	 	  		recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
	 	  		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
	 	  		recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
	 	  	
	 	  	
	     }

	     public void onClick(View arg0) {
         	  if (recording) { 
         		  
         		  recorder.stop();
         		  recorder.release();
         		 Intent i = new Intent(MediaVideoCameraActivity.this, MediaVideoCameraSaveActivity.class);
         		 i.putExtra("Point", point);
         		 i.putExtra("PATH", fullNamePath);
         		 i.putExtra("Point", point);
         		 startActivity(i);
         		  finish(); 
              } else {
            	
           	  recording = true;   
         	try {
         		recorder.reset();
         		initTheRecorder();
         		recorder.setPreviewDisplay(camcorderHolder.getSurface());
         		
           		recorder.setOutputFile(fullNamePath);
  				recorder.prepare();
  		        recorder.start();
  			  
  				
  			} catch (IllegalStateException e) {
  				Log.e("IllegalStateException", e.toString());
  			} catch (IOException e) {
  				Log.e("IOException", e.toString());
  			}	
  	      	  
	                  
	            reddot.setAlpha(100);
            	
              } 
		}

		public void surfaceChanged(SurfaceHolder holder, int format, int w,
				int h) {
			if(recording == false){ //recording
				
				recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/tempfile");

				try {
					recorder.prepare();
					recorder.start();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
				
			}
		}

		public void surfaceCreated(SurfaceHolder arg0) {
			
		}
		
		public void surfaceDestroyed(SurfaceHolder arg0) {
			// TODO Auto-generated method stub
			
		}	     
}